package com.rp.admin.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONUtil;
import com.rp.admin.serivce.FriendLinkService;
import com.rp.bo.SaveFriendLinkBO;
import com.rp.mo.FriendLinkMO;
import com.rp.news.common.Result;
import com.rp.news.util.RedisOperator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
@Api(value = "首页友情链接相关接口", tags = {"首页友情链接相关接口 api文档"})
@RequestMapping("/friendLinkMng")
public class FriendLinkMngController {


    @Autowired
    private FriendLinkService friendLinkService;

    @Autowired
    public RedisOperator redis;


    @ApiOperation(value = "新增或者修改友情链接", notes = "新增或者修改友情链接", httpMethod = "POST")
    @PostMapping("/saveOrUpdateFriendLink")
    public Result saveOrUpdateFriendLink(@RequestBody @Valid SaveFriendLinkBO saveFriendLinkBO) {
        log.info("新增或者修改友情链接,请求参数 saveFriendLinkBO = {}", JSONUtil.toJsonStr(saveFriendLinkBO));
        FriendLinkMO friendLinkMO = Convert.convert(FriendLinkMO.class, saveFriendLinkBO);
        friendLinkMO.setCreateTime(new Date());
        friendLinkMO.setUpdateTime(new Date());
        friendLinkService.saveOrUpdateFriendLink(friendLinkMO);
        return Result.ok(friendLinkMO);
    }

    @ApiOperation(value = "查询友情链接列表", notes = "查询友情链接列表", httpMethod = "POST")
    @PostMapping("/getFriendLinkList")
    public Result getFriendLinkList() {
        log.info("查询友情链接列表");
        List<FriendLinkMO> friendLinkMOS = friendLinkService.queryAllFriendList();
        return Result.ok(friendLinkMOS);
    }

    @ApiOperation(value = "删除友情链接", notes = "删除友情链接", httpMethod = "POST")
    @PostMapping("/delete")
    public Result delete(String linkId) {
        friendLinkService.delete(linkId);
        return Result.ok();
    }

    @ApiOperation(value = "门户端查询有情链接列表", notes = "门户端查询有情链接列表", httpMethod = "POST")
    @PostMapping("/portal/list")
    public Result queryPortalAllFriendLinkList() {
        log.info("门户端查询有情链接列表");
        List<FriendLinkMO> friendLinkMOS = friendLinkService.queryPortalAllFriendLinkList();
        return Result.ok(friendLinkMOS);
    }
}
